package com.briup.srms.sms.service.impl;

import java.util.Date;
import java.util.List;

import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.briup.srms.sms.bean.Leave;
import com.briup.srms.sms.bean.LeaveExample;
import com.briup.srms.sms.bean.extend.LeaveExtend;
import com.briup.srms.sms.dao.LeaveMapper;
import com.briup.srms.sms.dao.extend.LeaveExtendMapper;
import com.briup.srms.sms.service.ILeaveService;
import com.briup.srms.sms.service.IStudentAttendanceService;
import com.jacky.base.utils.CustomerException;
import com.jacky.base.utils.DateUtil;
import com.jacky.base.utils.PageVM;

@Service
public class LeaveServiceImpl implements ILeaveService {

	@Autowired
	private LeaveMapper leaveMapper;
	@Autowired
	private LeaveExtendMapper leaveExtendMapper;
	@Autowired
	private IStudentAttendanceService saService;
	
	@Override
	public void cancelApply(@NotNull Long id) {
		Leave leave = leaveMapper.selectByPrimaryKey(id);
		if(leave == null) {
			throw new CustomerException("要取消的请假不存在");
		}
		if(!leave.getStatus().equals("未审核")) {
			throw new CustomerException("请假状态不合法");
		}
		leaveMapper.deleteByPrimaryKey(id);
	}
	
	@Override
	public PageVM<LeaveExtend> pageQuery(int page, int pageSize, Long studentId,Long clazzId) {
		List<LeaveExtend> list = leaveExtendMapper.query(page, pageSize,studentId,clazzId);
		long total = leaveExtendMapper.count(studentId,clazzId);
		return new PageVM<>(page, pageSize, total, list);
	}

	@Override
	public void saveOrUpdate(Leave leave) throws CustomerException {
		if(leave.getId() == null) {
			// 判断请假日期是否超过了15天
			long days = DateUtil.daysBetween(leave.getBeginTime(), leave.getFinishTime());
			if(days > 15) {
				throw new CustomerException("一次请假时间不允许超过15天");
			}
			// 判断本次请假的日期是否与之前冲突
			LeaveExample example = new LeaveExample();
			example.createCriteria().andStudentIdEqualTo(leave.getStudentId());
			List<Leave> list = leaveMapper.selectByExample(example);
			if(list.size()>0) {
				for(Leave l : list) {
					long beginTime = leave.getBeginTime();
					long endTime = leave.getFinishTime();
					// leave新录入， l是历史
					if((l.getBeginTime() <= beginTime && l.getFinishTime() >=beginTime) || 
						(endTime >= l.getBeginTime() && endTime <=l.getFinishTime())) {
						throw new CustomerException("本次请假日期与之前请假日期冲突！");
					}
				}
			}
			
			leaveMapper.insert(leave);
		} else {
			leaveMapper.updateByPrimaryKey(leave);
		}
	}
	
	@Override
	public void deleteById(long id) throws CustomerException {
		Leave leave = leaveMapper.selectByPrimaryKey(id);
		if(leave == null) {
			throw new CustomerException("要删除的数据不存在");
		}
		leaveMapper.deleteByPrimaryKey(id);
	}
	
	@Override
	@Transactional
	public void checkApply(String status, Long[] leaveIds, String reason) {
		for(long leaveId : leaveIds) {
			Leave leave = leaveMapper.selectByPrimaryKey(leaveId);
			// 审核通过后插入出勤数据
			if(status.equals("审核通过")) {
				List<Long> days = DateUtil.parse(leave.getBeginTime(), leave.getFinishTime());
				for(long day : days) {
					String comment = leave.getComment();
					comment = comment.substring(1, comment.length()-1);
					String[] stages = comment.split(",");
					for(String stage : stages) {
						// 审核通过后插入出勤数据，考勤的维度是天、阶段
						Long[] ids = new Long[] {leave.getStudentId()};
						saService.batchSave(ids, day, stage, "请假",leave.getReason());
					}
				}
			}
			leave.setStatus(status);
			leave.setCheckReason(reason);
			leave.setCheckTime(new Date().getTime());
			// 更新请假数据
			leaveMapper.updateByPrimaryKey(leave);
		}
		
	}

}
